package com.purchase.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.purchase.entity.AppVersion;
import com.purchase.mapper.AppVersionMapper;
import com.purchase.service.AppVerService;
import com.purchase.util.MyBeanUtils;
import com.purchase.util.RequestForm;
import com.purchase.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AppVerServiceImpl implements AppVerService {
	@Resource
	AppVersionMapper appVerMapper;
	
	@Override
	public ResponseForm getVerInfo(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
//			String currentVerCode = (String) paramMap.get("VerCode");
			String appAlias = (String) paramMap.get("appAlias");
			try {
				AppVersion verInfo = appVerMapper.getLatestVer(appAlias);
//				if(app.getVerCode().equals(currentVerCode)) {
//					result.setMessage("当前程序版本已是最新版本");
//					return result;
//				}
				result.setCode("200");
				result.setStatus(true);
				result.setData(verInfo);
//				result.setMessage("发现新版本待更新,安装包大小为"+app.getAppSize()+"M");
				result.setMessage("操作成功");
			} catch (Exception e) {
				result.setCode("400");
				result.setStatus(true);
				result.setMessage("服务器错误");
				log.error("sql错误", e);
				e.printStackTrace();
			}
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm newVer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			AppVersion ver = MyBeanUtils.populate(new AppVersion(), paramMap);
			ver.setVerCode(UUID.randomUUID().toString().replace("-", ""));
			try {
				appVerMapper.insertSelective(ver);
				result.setCode("200");
				result.setStatus(true);
				result.setMessage("操作成功");
			} catch (Exception e) {
				result.setCode("400");
				result.setStatus(true);
				result.setMessage("服务器错误");
				log.error("sql错误", e);
				e.printStackTrace();
			}
		}
		return result;
	}
	public static void main(String[] args) {
//		Map paramMap = new HashMap<>();
//		paramMap.put("appName", "stock");
//		AppVersion populate = MyBeanUtils.populate(AppVersion.class, paramMap);
//		System.out.println(AppVersion.class.toString());
		String uuid = UUID.randomUUID().toString().replace("-", "");
		System.out.println(uuid);
	}

}
